package com.chen10.gobang.controller;

import com.chen10.gobang.common.enums.response.SuccessMsgEnum;
import com.chen10.gobang.common.exception.BusinessException;
import com.chen10.gobang.common.protocal.ResponseResult;
import com.chen10.gobang.common.protocal.ResponseVO;
import com.chen10.gobang.model.dto.FightInviteAcceptDTO;
import com.chen10.gobang.model.dto.FightInviteDTO;
import com.chen10.gobang.model.dto.PlayerReadyDTO;
import com.chen10.gobang.service.FightService;
import com.chen10.gobang.ws.pojo.Room;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@RequestMapping("/api/fight")
@Slf4j
public class FightController {
    @Resource
    private FightService fightService;

    /**
     * 邀请好友
     * @param fightInviteDTO
     * @return
     */
    @PostMapping("/invite")
    @RateLimiter(name = "apiResource", fallbackMethod = "rateLimiterFallback")
    public ResponseVO invite(@RequestBody FightInviteDTO fightInviteDTO) {
        log.info("invite: {}", fightInviteDTO);
        try {
            fightService.invite(fightInviteDTO);
            return ResponseResult.success(SuccessMsgEnum.INVITE_SUCCESS);
        }catch (BusinessException e){
            return ResponseResult.error(e.getCode(), e.getMessage());
        }
    }

    /**
     * 接受邀请
     * @param acceptDTO
     * @return null
     */
    @PostMapping("/accept")
    public ResponseVO accept(@RequestBody FightInviteAcceptDTO acceptDTO) {
        log.info("accept: acceptDTO{}", acceptDTO);
        try {
            fightService.accept(acceptDTO);
            return ResponseResult.success(SuccessMsgEnum.ACCEPT_INVITE_SUCCESS);
        }catch (BusinessException e){
            return ResponseResult.error(e.getCode(), e.getMessage());
        }
    }
    /**
     * 创建房间
     * @return
     */
    @PostMapping("/room/create")
    public ResponseVO<Room> createRoom() {
        try{
            return ResponseResult.success(SuccessMsgEnum.CREATE_ROOM_SUCCESS.getMsg(),
                    fightService.createRoom());
        }catch (BusinessException e){
            return ResponseResult.error(e.getCode(), e.getMessage());
        }
    }

    /**
     * 退出房间
     * @return
     */
    @PostMapping("/room/quit/{roomId}")
    public ResponseVO quitRoom(@PathVariable String roomId) {
        try{
            fightService.quitRoom(roomId);
            return ResponseResult.success(SuccessMsgEnum.QUIT_ROOM_SUCCESS);
        }catch (BusinessException e){
            return ResponseResult.error(e.getCode(), e.getMessage());
        }
    }

    /**
     * 开始游戏
     * @param roomId
     * @return
     */
    @PostMapping("/game/start/{roomId}")
    public ResponseVO startGame(@PathVariable String roomId) {
        try{
            fightService.startGame(roomId);
            return ResponseResult.success(SuccessMsgEnum.START_GAME_SUCCESS);
        }catch (BusinessException e){
            return ResponseResult.error(e.getCode(), e.getMessage());
        }
    }

    /**
     * 玩家准备
     * @param readyDTO
     * @return
     */
    @PostMapping("/room/ready")
    public ResponseVO ready(@RequestBody PlayerReadyDTO readyDTO) {
        try{
            fightService.ready(readyDTO);
            return ResponseResult.success(SuccessMsgEnum.READY_SUCCESS);
        }catch (BusinessException e){
            return ResponseResult.error(e.getCode(), e.getMessage());
        }
    }


}
